﻿<html>
<body>

<?php
require 'weather.php';
require 'location.php';

date_default_timezone_set('Asia/Hong_Kong');  //set time zone
set_error_handler("myHandler");               //set error handler
$chinatime = date('Y-m-d H:i:s');             //get current time
$max_size = 500000;

try
{
	emptyUserRecord();
    $funcFlag = 0;
    $keyword = "0755";
    $resultStr = "";
    $cityArray = array();
    $contentStr = "";
    $needArray = false;

    if ($keyword === "Hello2BizUser"){
        $contentStr = "你好，欢迎关注天气神。\n微信平台上最强大最好用最受欢迎的天气查询应用\n获取使用方法请发送 ? 或 help 或 sos 或 “帮助”。";
    }
    else if(preg_match("/^\?|？|help|sos|帮助$/",strtolower($keyword))){
        //$contentStr = "★★★★天气神★★★★ 微信平台上最强大最好用最受欢迎的天气查询应用\n使用方法：\n1.发送城市名,如“深圳”;\n2.发送电话区号,如“0755”;\n3.发送全拼,如“shenzhen”;\n4.发送简拼,如“sz”;\n5.发送邮政编码,如518057;\n6.发送位置,点击“＋”,然后选择“位置”,等待微信确定你的位置后,点击“发送”;\n7.发送语音,点击“按住说话”,说出你的地址,然后松开结束录音;\n8.更多玩法,敬请期待……";
        $contentStr = "★★★★天气神★★★★ 微信平台上最强大最好用最受欢迎的天气查询应用\n使用方法：\n1.发送城市名,如“深圳”;\n2.发送电话区号,如“0755”;\n3.发送全拼,如“shenzhen”;\n4.发送邮政编码,如518000;\n…\n99.更多玩法,敬请期待……";
    }
    //提交城市
    else if(preg_match("/^\+[\x{4e00}-\x{9fa5}]+$/u",$keyword)){
        $contentStr = "[".substr($keyword,1,strlen($keyword)).'] 已提交，我们将尽快处理，谢谢您的支持';
        $funcFlag = 1;
    }
    //电话号码区号
    else if(preg_match("/^\d{3,5}$/",$keyword)){
        echo($keyword.'是电话号码区号');
        $cityArray = fromKeywordToCode("teleCode", $keyword);
    }
    //邮政编码
    else if (preg_match("/^\d{6}$/",$keyword)){
        echo($keyword.'是邮政编码');
        $cityArray = fromKeywordToCode("postCode", $keyword);
    }
    // else if (preg_match("/^[A-Za-z]{2}$/",$keyword)){
        // echo($keyword.'是城市简拼');
    // }
    //城市全拼
    else if (preg_match("/^[A-Za-z]{3,}$/",$keyword)){
        echo($keyword.'是城市全拼');
        $cityArray = fromKeywordToCode("quanPing", $keyword);
    }
    //是汉字
    else if (preg_match("/^[\x{4e00}-\x{9fa5}]+$/u",$keyword)){
        echo($keyword.'是汉字');
        $cityArray = fromKeywordToCode("cityName", $keyword);
    }
    else {
        $contentStr = $keyword.'不是合法的查询指令';
        $funcFlag = 1;
    }
    
    logger("cityArray"."<br>");
    var_dump($cityArray);
    logger("contentStr"."<br>");
    var_dump($contentStr);
    
    echo("<br>");
    echo("<br>");
    
    if (count($cityArray) == 0) {
        if ($contentStr == ""){
            $contentStr = "很抱歉，[".$keyword."]未被收录！如你确定它是有效查询指令，请发送  +".$keyword."  我们将收录它，谢谢您的支持！";
            var_dump($contentStr);
        }
        else {
            logger("EMPTY REPLY: ".$keyword);
            
        }
    }
    else if (count($cityArray) == 1){
        if (preg_match("/^北京|上海|广州|深圳$/u",$keyword)){
            $needArray = true;
        }
        $cityCode = current($cityArray);
        $weatherRealTime = getWeather_realTime($cityCode, $needArray);
        var_dump($weatherRealTime);
        $weatherSixDays = getWeather_sixdays($cityCode, $needArray);
        var_dump($weatherSixDays);
        $copyRight = copyRight($needArray);
        var_dump($copyRight);
        
        //var_dump(array_merge_recursive($weatherRealTime, $weatherSixDays, $copyRight));

    }
    else {
        $cities = "匹配多个，请再次输入：";
        while(list($key, $val)= each($cityArray)) {
            $cities .= "\n".$key;
        }
        $contentStr = $cities;
        var_dump($contentStr);
    }
        
        
        
    // echo(count($cityArray));
    // if (count($cityArray) == 0) {
        // echo('cityArray = 0');
        // //echo($cityArray[cityCode]);
    // }
    // else if (count($cityArray) == 1){
        // echo(current($cityArray));
    // }
    // else {
        // $cities = "匹配多个，请再次输入：";
        // while(list($key, $val)= each($cityArray)) {
            // $cities .= $key."\n";
        // }
        // echo($cities);
    // }

    
/* 	//fromKeywordToCode();
    $arr = array('Hello','World!','Beautiful','Day!');
    echo implode("\n",$arr);
    

    $needArray = false;
    $weatherRealTime = getWeather_realTime($cityCode, $needArray);
    $weatherSixDays = getWeather_sixdays($cityCode, $needArray);
    $copyRight = copyRight($needArray);
    // var_dump($weatherRealTime);
    // echo implode("\n",$weatherRealTime);
    
    if(is_array($weatherRealTime) && is_array($weatherSixDays)){
        //echo implode("<br>",array_merge($weatherRealTime, $weatherSixDays));
        logger(transmitNews("1",array_merge_recursive($weatherRealTime, $weatherSixDays, $copyRight),1)); 
    }else{
        echo $weatherRealTime.$weatherSixDays.$copyRight;
    } */
    
}
catch(Exception $e)
{
    logger("Exception Message: ".$e->getMessage());
}

//error handler function
function myHandler($level, $message, $file, $line, $context)
{
    logger("<b>[ERROR]</b> LEVEL: $level, MESSAGE: $message, FILE: $file, LINE: $line, CONTENT: $context");
    die();
}

function logger($log_content)
{
    Global $max_size;
    $log_filename = date("Ymd").".xml";
    if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);sleep(1);}
    file_put_contents($log_filename, date('H:i:s').$log_content."\r\n", FILE_APPEND);
}

function customReply($keyword)
{
    if ((double)$keyword > 0 and (double)$keyword < 200){
        return "Message: ".$keyword;
    }else{
        return "Unknown: ".$keyword;
    }
}

function copyRight($returnArray)
{
    $copyRightArray = array(); 
    $copyRightArray[] = array(
        "title"=>"天界神仙政府驻腾讯微信大使天气神", "description"=>"", "pic"=>"", "url" =>"");
    if($returnArray){
        return $copyRightArray;
    }else {
        return $copyRightArray[0]["title"];
    }
}

?>

</body>
</html>